package com.LA_houseLease.admin.service.impl;


import com.LA_houseLease.admin.mapper.SystemUserMapper;
import com.LA_houseLease.admin.mapper.UserInfoMapper;
import com.LA_houseLease.admin.service.LoginService;
import com.LA_houseLease.admin.vo.login.CaptchaVo;
import com.LA_houseLease.admin.vo.login.LoginVo;
import com.LA_houseLease.admin.vo.system.user.SystemUserInfoVo;
import com.LA_houseLease.common.constant.RedisConstant;
import com.LA_houseLease.common.exception.houseLeaseException;
import com.LA_houseLease.common.properties.JwtProperties;
import com.LA_houseLease.common.result.ResultCodeEnum;
import com.LA_houseLease.common.utils.JwtUtil;
import com.LA_houseLease.pojo.entity.SystemUser;
import com.LA_houseLease.pojo.entity.UserInfo;
import com.LA_houseLease.pojo.enums.BaseStatus;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.wf.captcha.SpecCaptcha;
import org.apache.commons.codec.digest.DigestUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;

import java.util.Random;
import java.util.UUID;
import java.util.concurrent.TimeUnit;

@Service
public class LoginServiceImpl implements LoginService {

    @Autowired
    private StringRedisTemplate stringRedisTemplate;
    @Autowired
    private SystemUserMapper systemUserMapper;
    @Autowired
    private JwtProperties jwtProperties;
    /**
     * 获取图形验证码
     * @return
     */
    @Override
    public CaptchaVo getCaptcha() {
        //使用的是png类型
        SpecCaptcha specCaptcha=new SpecCaptcha(130,48,5);
        String value = specCaptcha.text().toLowerCase(); //获取验证码的字符
        String key= RedisConstant.ADMIN_LOGIN_PREFIX+ UUID.randomUUID(); //自定义生成key
        String image=specCaptcha.toBase64(); //对图像进行base64编码
        //向redis中存入图片验证码的key和value以及TTL(过时时长)
        stringRedisTemplate.opsForValue().set(key,value,RedisConstant.ADMIN_LOGIN_CAPTCHA_TTL_SEC, TimeUnit.SECONDS);
        return new CaptchaVo(image,key);
    }

    /**
     * 登录
     * @param loginVo
     * @return
     */
    @Override
    public String login(LoginVo loginVo) {
        if(loginVo.getCaptchaCode()==null){
            throw new houseLeaseException(ResultCodeEnum.APP_LOGIN_CODE_EMPTY);
        }
        if(stringRedisTemplate.opsForValue().get(loginVo.getCaptchaKey())==null){
            throw new houseLeaseException(ResultCodeEnum.ADMIN_CAPTCHA_CODE_EXPIRED);
        }
        if(!loginVo.getCaptchaCode().toLowerCase().equals(stringRedisTemplate.opsForValue().get(loginVo.getCaptchaKey()))){
            throw new houseLeaseException(ResultCodeEnum.ADMIN_CAPTCHA_CODE_ERROR);
        }
        LambdaQueryWrapper<SystemUser> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(SystemUser::getUsername,loginVo.getUsername());
        SystemUser systemUser = systemUserMapper.selectOne(lambdaQueryWrapper);
        if(systemUser==null){
            throw new houseLeaseException(ResultCodeEnum.ADMIN_ACCOUNT_NOT_EXIST_ERROR);
        }
        if(systemUser.getStatus()== BaseStatus.DISABLE){
            throw new houseLeaseException(ResultCodeEnum.ADMIN_ACCOUNT_DISABLED_ERROR);
        }
        String password=systemUserMapper.selectPassword(systemUser.getId());
        if(!DigestUtils.md5Hex(loginVo.getPassword()).equals(password)){
            throw new houseLeaseException(ResultCodeEnum.ADMIN_ACCOUNT_ERROR);
        }
        return JwtUtil.creatToken(jwtProperties.getSecretKey(),jwtProperties.getTtl(),systemUser.getId(),systemUser.getUsername());
    }

    /**
     * 获取登陆用户个人信息
     * @param userId
     * @return
     */
    @Override
    public SystemUserInfoVo getSystemUserInfoVoByUserId(Long userId) {
        SystemUser systemUser = systemUserMapper.selectById(userId);
        SystemUserInfoVo systemUserInfoVo=new SystemUserInfoVo();
        BeanUtils.copyProperties(systemUser,systemUserInfoVo);
        return systemUserInfoVo;
    }
}




















